// facility/SensorData.js
import http from '../../http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowfacilityInfo: '', //页面传参
    detailvalue: 108,
    option1: [
      {
        text: '甲烷温度阙值设置',
        value: 108
      },
      {
        text: '检测周期设置',
        value: 106
      },
      {
        text: '上报周期设置',
        value: 107
      }
      // ,
      // {
      //   text: '水浸阙值设置',
      //   value: 110
      // },
      
    ],
    realData:'',//实时参数
    collect_time_set: undefined, //检测周期
    updata_time_set: undefined, //数据上报周期
    methane_threshold_set: undefined, //甲烷浓度
    TEMPH_threshold_set: undefined, //高温阈值
    TEMPL_threshold_set: undefined, //低温阈值
    water_threshold_set: undefined, //水浸阈值
  },
  onSwitch1Change({
    detail
  }) {
    this.setData({
      detailvalue: detail
    })
    console.log(detail);
  },
  // 实时参数
  parameter() {
      http.get({
        url: '/zg/iot-links/data/latest/' + this.data.nowfacilityInfo.id,
        data: {
          // id: this.properties.nowfacilityInfo.id
        },
        then: (res) => {
          console.log('设备最新数据', res);
          let list = {}
          res.data.forEach(item => {
            list = {
              ...list,
              ...item.formatValue
            }
          })
          // console.log(list.device_location.split(','));
          list.device_lng = list.device_location.split(',')[0]
          list.device_lat = list.device_location.split(',')[1]
          list.sensor_voltage = list.sensor_battery.split(',')[0]
          list.sensor_quantity = list.sensor_battery.split(',')[1]
          this.setData({
            realData: list,
            collect_time_set:list.device_collect_time,
            updata_time_set:list.device_updata_time,
            methane_threshold_set:list.methane_threshold,
            TEMPH_threshold_set:list.TEMPH_threshold,
            TEMPL_threshold_set:list.TEMPL_threshold,
            water_threshold_set:list.water_threshold,
          })
          console.log(this.data.realData);
        }
      })
    

  },
  //提交检测周期
  submit(date, code) {
    const data = {
      command: "LAMP_CONTROL",
      brightness: 0,
      deviceId: this.data.nowfacilityInfo.id,
      controlJson: {
        header: {
          code: code,
          device_ID: this.data.nowfacilityInfo.sn
        },
        body: date
      }
    }
    wx.showLoading({
      title: '加载中...'
    });
    http.post({
      url: '/zg/command/one',
      data: data,
      then: (res) => {
        console.log('回路周期返回', JSON.parse(res.data.data), res);
        wx.hideLoading()
        if (res.data.code == 0) {
          wx.showToast({
            title: '下发成功',
            icon: 'success',
          })
        }
      }
    })
  },
  ParameterSumbitBot() {
    if (this.data.detailvalue == 106) {
      this.detectionPeriod()
    } else if (this.data.detailvalue == 107) {
      this.ReportingCycle()
    } else if (this.data.detailvalue == 108) {
      this.gasParameter()
    } else if (this.data.detailvalue == 110) {
      this.FloodSubmit()
    }
  },
  detectionPeriod() {
    if (!this.data.collect_time_set) {
      wx.showToast({
        title: '请输入检验周期',
        icon: 'error',
      })
    } else {
      const data = {
        collect_time_set:parseInt(this.data.collect_time_set) 
      }
      this.submit(data, 106)
    }

  },
  ReportingCycle() {
    if (!this.data.updata_time_set) {
      wx.showToast({
        title: '请输入上报周期',
        icon: 'error',
      })
    } else {
      const data = {
        updata_time_set: parseInt(this.data.updata_time_set)
      }
      this.submit(data, 107)
    }
  },
  gasParameter() {
    if (!this.data.methane_threshold_set || !this.data.TEMPH_threshold_set || !this.data.TEMPL_threshold_set) {
      wx.showToast({
        title: '请输入完整参数',
        icon: 'error',
      })
    } else {
      const data = {
        methane_threshold_set: parseInt(this.data.methane_threshold_set),
        TEMPH_threshold_set: parseInt(this.data.TEMPH_threshold_set),
        TEMPL_threshold_set: parseInt(this.data.TEMPL_threshold_set)
      }
      this.submit(data, 108)
    }
  },
  FloodSubmit() {
    if (!this.data.water_threshold_set) {
      wx.showToast({
        title: '请输入水浸值',
        icon: 'error',
      })
    } else {
      const data = {
        water_threshold_set: this.data.water_threshold_set
      }
      this.submit(data, 110)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      nowfacilityInfo: JSON.parse(options.nowfacilityInfo)
    })
    this.parameter()
    console.log(JSON.parse(options.nowfacilityInfo), 'options');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})